package com.briup.labs.service.impl;

import com.briup.labs.mapper.SmartLabsMapper;
import com.briup.labs.pojo.*;
import com.briup.labs.service.SmartLabsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SmartLabsServiceImpl implements SmartLabsService {

    @Autowired
    private SmartLabsMapper smartLabsMapper;

    @Override
    public List<SmartLabs> all() {
        return smartLabsMapper.all();
    }

    @Override
    public List<SmartLabs> list(String key) {
        return smartLabsMapper.list(key);
    }

    @Override
    public PageBean<Labs> list(String pageNum, String pageSize, String key) {
        //创建PageBean对象
        PageBean<Labs> pageBean = new PageBean<>();

        //检测数据输入格式
        int PN = 0;
        int PS = 0;
        try {
            PN = Integer.parseInt(pageNum);
            PS = Integer.parseInt(pageSize);
        } catch (Exception e) {
            pageBean.setTotal(null);
            pageBean.setItems(null);
            return pageBean;
        }

        //开启分页查询 PageHelper
        PageHelper.startPage(PN, PS);

        //调用mapper
        List<SmartLabs> smartLabs = smartLabsMapper.list(key);

        //创建最后的数据集合
        List<Labs> list = new ArrayList<>();
        for (SmartLabs smartLab : smartLabs) {
            Labs labs = new Labs();
            //加入实验室数据
            labs.setId(smartLab.getId());
            labs.setLabsName(smartLab.getLabsName());
            labs.setInfos(smartLab.getInfos());
            labs.setNetAdres(smartLab.getNetAdres());
            labs.setPort(smartLab.getPort());
            labs.setLabsNum(smartLab.getLabsNum());
            //加入用户数据
            SysUser sysUser = smartLabsMapper.getUser(smartLab.getId());
            labs.setSysUser(sysUser);
            list.add(labs);
        }

        //Page中提供了方法，可以获取PageHelper分页查询后，得到的总记录条数和当夜数据
        //Page<Labs> page = (Page<Labs>) list;

        PageInfo<Labs> pageInfo = new PageInfo<>(list);

        //把数据填充到PageBean对象中
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setItems(pageInfo.getList());
        return pageBean;
    }

    @Override
    public void saveOrUpdate(SmartLabs smartLabs) {
        //数据表没有设置自增
        List<SmartLabs> all = smartLabsMapper.all();
        for (SmartLabs smartLab : all) {
            if (Objects.equals(smartLab.getId(), smartLabs.getId())) {
                //修改
                smartLabsMapper.update(smartLabs);
                return;
            }
        }
        //添加
        smartLabsMapper.add(smartLabs);
    }

    @Override
    public void saveOrUpdateLabsAndUser(SmartLabsAdminR smartLabsAdminR) {
        //数据表没有设置自增
        List<SmartLabsAdminR> admins = smartLabsMapper.admins();
        for (SmartLabsAdminR admin : admins) {
            if (Objects.equals(smartLabsAdminR.getId(), admin.getId())) {
                //修改
                smartLabsMapper.updateAdmin(smartLabsAdminR);
                return;
            }
        }
        //添加
        smartLabsMapper.addAdmin(smartLabsAdminR);
    }

    @Override
    public void delete(Integer id) {
        //删除labs表中的数据
        smartLabsMapper.delete(id);
        //删除labs-admin桥表中的数据
        smartLabsMapper.deleteBridgeAdmin(id);
        //删除用到该lab的device
        smartLabsMapper.deleteDevice(id);
    }
}
